package com.hl.sfzy.controller;

import com.hl.sfzy.utils.CaptchaUtil;
import com.hl.sfzy.utils.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CaptchaController {

    @GetMapping(value = "/captcha")
    public R getCaptcha() throws IOException {
        String captcha = CaptchaUtil.generateCaptcha();
        String[] parts = captcha.split("&");
        String question = parts[0];
        byte[] image = CaptchaUtil.createCaptchaImage(question);
        // 这里可以将答案存到Map集合中，响应给前端，用于验证
        Map map = new HashMap();
        map.put("img", Base64.getEncoder().encodeToString(image));
        map.put("answer",parts[1]);
        return R.ok(map);
    }
}
